package cn.edu.xaut.mybatisers.mall.ServiceImp;

import cn.edu.xaut.mybatisers.mall.Entity.Commodity;
import cn.edu.xaut.mybatisers.mall.Entity.Favourite;
import cn.edu.xaut.mybatisers.mall.Entity.OrderOneByOne;
import cn.edu.xaut.mybatisers.mall.Entity.ShoppingTrolley;
import cn.edu.xaut.mybatisers.mall.Dao.ShoppingTrolleyDao;
import cn.edu.xaut.mybatisers.mall.Service.ShoppingTrolleyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShoppingTrolleyServiceImp implements ShoppingTrolleyService {
    @Resource
    ShoppingTrolleyDao shoppingTrolleyDao;


    @Override
    public void addCommodity(ShoppingTrolley shoppingTrolley) throws SQLException {
        if(shoppingTrolleyDao.selectByTwoId(shoppingTrolley).size() > 0){
            if (shoppingTrolley.getNumber() != 0) {
                shoppingTrolleyDao.updateNumber(shoppingTrolley);
            }else {
                shoppingTrolleyDao.deleteCommodity(shoppingTrolley);
            }
        }else {
            shoppingTrolleyDao.addCommodity(shoppingTrolley);
        }
    }

    @Override
    public Map<String, Object> countPrice(String orderNumber, List<Integer> commodity_ids, HttpSession session) {
        int consumer_id = (Integer)session.getAttribute("consumer_id");
        List<ShoppingTrolley> shoppingTrolleys =  countPrice(consumer_id); //查找该用户购物车记录
        List<ShoppingTrolley> deleteShoppingTrolleys = new ArrayList<>();
        for (ShoppingTrolley st : shoppingTrolleys)
            if (!commodity_ids.contains(st.getCommodity_id()))
                deleteShoppingTrolleys.add(st);
        for (ShoppingTrolley st : deleteShoppingTrolleys)
            shoppingTrolleys.remove(st);

        List<OrderOneByOne> orderOneByOnes = new ArrayList<>();
        double sum = 0;
        double c;
        Commodity commodity;
        for (ShoppingTrolley shoppingTrolley : shoppingTrolleys) {
            commodity = shoppingTrolleyDao.findCommodityById(shoppingTrolley.getCommodity_id());
            c = commodity.getPrice() * shoppingTrolley.getNumber();
            if (session.getAttribute("isMember") == "1")
                c *= commodity.getDiscount();

            OrderOneByOne orderOneByOne = new OrderOneByOne();
            orderOneByOne.setOrder_number(orderNumber);
            orderOneByOne.setCommodity_id(shoppingTrolley.getCommodity_id());
            orderOneByOne.setBuy_number(shoppingTrolley.getNumber());
            orderOneByOne.setDealPrice((float) c);
            orderOneByOnes.add(orderOneByOne);
            sum += c;
        }
        Map<String, Object> data = new HashMap<>();
        data.put("totalPrice", String.valueOf(sum));
        data.put("orderOneByOne", orderOneByOnes);
        String s = commodity_ids.toString();
        s = s.substring(1, s.length()-1);
        shoppingTrolleyDao.emptyCart(s, consumer_id);
        return data;
    }

    @Override
    public List<HashMap<String, Object>>  findCommodity(int pageNum,int pageSize, HttpSession session) {
        PageHelper.startPage(pageNum, pageSize);
        List<HashMap<String, Object>> commodities = shoppingTrolleyDao.findCommodityInTrolley((Integer) session.getAttribute("consumer_id"));
        return commodities;
    }

    @Override
    public List<ShoppingTrolley> countPrice(int consumer_id) {
        return shoppingTrolleyDao.countPrice(consumer_id);
    }


//    @Override
//    public void deleteCommodity(ShoppingTrolley shoppingTrolley) {
//        shoppingTrolleyDao.deleteCommodity(shoppingTrolley);
//    }
//
//    @Override
//    public void incCommodityNumber(int id) {
//        shoppingTrolleyDao.incCommodityNumber(id);
//    }
//
//    @Override
//    public void subCommodityNumber(int id) {
//        shoppingTrolleyDao.subCommodityNumber(id);
//    }
//
//    @Override
//    public void emptyCart(int consummer_id) {
//        shoppingTrolleyDao.emptyCart(consummer_id);
//    }
//
//
//    @Override
//    public double findPrice(int id) {
//        return shoppingTrolleyDao.findPrice(id);
//    }
//
//    @Override
//    public double findCount(int id) {
//        return shoppingTrolleyDao.findCount(id);
//    }
//
//    @Override
//    public boolean isMember(int id) {
//        return shoppingTrolleyDao.isMember(id);
//    }
//
//    @Override
//    public void addOne(ShoppingTrolley shoppingTrolley) {
//        shoppingTrolleyDao.addOne(shoppingTrolley);
//    }
//
//    @Override
//    public int checkNumber(ShoppingTrolley shoppingTrolley) {
//        return shoppingTrolleyDao.checkNumber(shoppingTrolley);
//    }
//
//    @Override
//    public void subOne(ShoppingTrolley shoppingTrolley) {
//        shoppingTrolleyDao.subOne(shoppingTrolley);
//    }
//
//    @Override
//    public List<Favourite> isFempty(Favourite f) {
//        return shoppingTrolleyDao.isFempty(f);
//    }

}
